package com.wanmait.bootdemo.filter;

import com.wanmait.bootdemo.pojo.QuestionType;
import com.wanmait.bootdemo.service.QuestionTypeService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@WebFilter("/*")
public class LoginFilter implements Filter {
    //@Resource
    //private QuestionTypeService questionTypeService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //原来的SSM项目中 springboot项目中 获得bean的方式
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletRequest.getServletContext());
        QuestionTypeService questionTypeService = applicationContext.getBean("questionTypeServiceImpl",QuestionTypeService.class);

        List<QuestionType> questionTypes = questionTypeService.findAll();
        questionTypes.forEach(System.out::println);

        System.out.println("loginFilter...................");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        System.out.println(request.getRequestURL());
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
